package com.beifu.manager.controller;

import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.model.AssetType;
import com.beifu.manager.model.AssetTypeExample;
import com.beifu.manager.service.assettype.AssetTypeService;
import com.mds.manager.utils.ListUtils;
import com.mds.manager.utils.PageUtils;
import com.mds.manager.utils.ResponseHelp;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * Created by 再壮 on 2018/6/19.
 */
@Controller
@RequestMapping(value="/baseType", produces="text/html;charset=UTF-8")
public class AssetTypeController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private AssetTypeService assetTypeService;

    @Autowired
    private AssetTypeMapper assetTypeMapper;

    @RequestMapping("/typeIndex")
    public String goUserIndex(Model model){
        log.info("进入资产类别管理界面");
        return "/base/assettype/type_index";
    }

    /**
     * 资产类别列表分页数据
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTypePage")
    public String getTypePage(Model model, HttpServletRequest request){
        log.info(">>> 进入资产类别分页列表");
        String page = request.getParameter("pageNumber");
        String name = request.getParameter("assetsName");
        String modelName = request.getParameter("modelName");

        AssetTypeExample example = new AssetTypeExample();
        AssetTypeExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        if (!StringUtils.isEmpty(modelName)) {
            criteria.andModelNameLike("%"+modelName+"%");
        }

        PageUtils Page = new PageUtils(page);
        Page.setQueryParames(example);
        Page = assetTypeService.getDataPages(Page);
        return ResponseHelp.responseText(Page);
    }

    @RequestMapping("/goAddTypePage")
    public String goAddTypePage(Model model){
        log.info("进入用户添加界面");
        return "/base/assettype/type_add";
    }

    /**
     * 资产是否存在
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/isExist")
    public String isExist(Model model,HttpServletRequest request) {
        String assetsName = request.getParameter("assetsName");
        String modelName = request.getParameter("modelName");

        AssetTypeExample example = new AssetTypeExample();
        AssetTypeExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(assetsName).andModelNameEqualTo(modelName);
        List<AssetType> list = assetTypeMapper.selectByExample(example);
        if (ListUtils.isNotNull(list)) {
            return ResponseHelp.responseErrorText("该资产已存在！");
        }
        return ResponseHelp.responseText();
    }

    /**
     * 添加资产
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/typeAdd")
    public String typeAdd(Model model,HttpServletRequest request){
        String assetsName = request.getParameter("assetsName");
        String modelName = request.getParameter("modelName");

        AssetType assetType = new AssetType();
        assetType.setName(assetsName);
        assetType.setModelName(modelName);
        assetTypeService.insert(assetType);

        return ResponseHelp.responseText();
    }

    @RequestMapping("/goUpdateTypePage")
    public String goUpdateTypePage(Model model,HttpServletRequest request){
        log.info("进入用户修改界面");

        String id = request.getParameter("id");
        AssetType user = assetTypeMapper.selectByPrimaryKey(Integer.decode(id));

        model.addAttribute("user", user);
        return "/base/assettype/type_update";
    }

    /**
     * 更新资产
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/typeUpdate")
    public String typeUpdate(Model model,HttpServletRequest request){
        String id = request.getParameter("id");
        String assetsName = request.getParameter("assetsName");
        String modelName = request.getParameter("modelName");

        AssetType assetType = new AssetType();
        if (StringUtils.isBlank(id)) {
            return ResponseHelp.responseErrorText("请选择需要修改的条目！");
        }

        assetType.setId(Integer.decode(id));
        assetType.setName(assetsName);
        assetType.setModelName(modelName);

        assetTypeService.updateByPrimaryKey(assetType);

        return ResponseHelp.responseText();
    }

    /**
     * 删除资产
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/typeDel")
    public String typeDel(Model model,HttpServletRequest request){
        String ids = request.getParameter("ids");
        String[] idsArr = ids.split(",");
        assetTypeService.batchDel(idsArr);
        return ResponseHelp.responseText();
    }

}
